<template>
  <router-view />
</template>

<script setup>
import useSettingsStore from '@/store/modules/settings'
import { handleThemeStyle } from '@/utils/theme'
import {countAudit, getGainAudit} from "@/api/audit/audit";
import logo from "@/assets/images/login.jpeg";
import {nextTick, onDeactivated, onMounted, ref} from "vue";
import {getToken} from "@/utils/auth";

onMounted(() => {
  nextTick(() => {
    // 初始化主题样式
    handleThemeStyle(useSettingsStore().theme)
  })
})
function notification() {
    if (window.Notification) {

        // 浏览器通知--window.Notification
        if (Notification.permission == "granted") {
            countAudit().then(response => {
                if (response.data > 0 ){
                    popNotice('待审核人员', `您今日有待报销审核${response.data}人`)
                }
            });
        }else if( Notification.permission != "denied"){
            console.log("需要通知权限")
            Notification.requestPermission((permission)=> {});
        }
    } else {
        console.error('浏览器不支持Notification');
    }
}
function popNotice(user, content) {
    let that = this;
    if (Notification.permission == "granted") {
        let notification = new Notification(user, {
            body: content,
            icon: logo
        });

        notification.onclick = function(e) {
            that.$nextTick(() => {
                setTimeout(()=>{
                    //具体操作
                    //例如:
                    window.open("https://www.baidu.com","_brank")
                },3);
            });
            //可直接打开通知notification相关联的tab窗
            window.focus();
            notification.close();
        };
    }
}
function  setup() {
    const timer = ref(0)
    const index = ref(0)
    onMounted(()=>{ //组件挂载时的生命周期执行的方法
        timer.value = window.setInterval(function logname() {
            // 其他定时执行的方法
            index.value = index.value + 1
            notification()
        }, 60000);
    })
    onDeactivated(()=>{ //离开当前组件的生命周期执行的方法
        window.clearInterval(timer.value);
    })
}
function handleGain(){
    getGainAudit().then(response => {
        console.log(JSON.stringify(response))
    });
}

function  setupGain() {
    const timer = ref(0)
    const index = ref(0)
    onMounted(()=>{ //组件挂载时的生命周期执行的方法
        timer.value = window.setInterval(function logname() {
            // 其他定时执行的方法
            index.value = index.value + 1
            handleGain()
        }, 15000);
    })
    onDeactivated(()=>{ //离开当前组件的生命周期执行的方法
        window.clearInterval(timer.value);
    })
}


function ifGetTOken() {
    if (getToken() == undefined){
        return
    }
    notification()
    setup()
}
ifGetTOken()
</script>
